package com.liuyu.common.framework.spring;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.NameMatchMethodPointcutAdvisor;

/**
 * 1
 * @author: liuyu
 * @date: 2021/8/25 12:56
 * @version:
 */
public class NameMatchMethodPointut {
    public static void main(String[] args) {
        ProxyFactory factory = new ProxyFactory(new Person());





        // 声明一个通知（此处使用环绕通知 MethodInterceptor ）
        Advice advice = (MethodInterceptor) invocation -> {
            System.out.println("============>放行前拦截...");
            Object obj = invocation.proceed();
            System.out.println("============>放行后拦截...");
            return obj;
        };
//        声明一个aspectj切点,一张切面
        //NameMatchMethodPointcut cut = new NameMatchMethodPointcut();
        //cut.setMappedName("run"); //会匹配所有的方法名为run的方法
//        切点+通知
        //Advisor advisor = new DefaultPointcutAdvisor(cut, advice);

        NameMatchMethodPointcutAdvisor advisor = new NameMatchMethodPointcutAdvisor();
        advisor.setMappedName("run");
        advisor.setAdvice(advice);

        factory.addAdvisor(advisor);
        Person p = (Person) factory.getProxy();

        // 执行方法
        p.run();
        p.run(10);
        p.say();
        p.sayHi("Jack");
        p.say("Tom", 666);

    }
}
